package com.it135.citytraffic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it135.citytraffic.mapper.SurveyMapper;
import com.it135.citytraffic.pojo.entity.Survey;
import com.it135.citytraffic.pojo.vo.SurveyVO;
import com.it135.citytraffic.service.ISurveyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 调查问卷表 服务实现类
 * </p>
 *
 * @author MK
 * @since 2024-07-06
 */
@Service
@RequiredArgsConstructor
public class SurveyServiceImpl extends ServiceImpl<SurveyMapper, Survey> implements ISurveyService {

    private final SurveyMapper surveyMapper;

    /**
     * 新增调查问卷填写信息
     *
     * @param surveys
     */
    @Override
    public void save(List<Survey> surveys) {
        for (int i = 0; i < surveys.size(); i++) {
            Survey survey = surveys.get(i);
            survey.setCreateTime(LocalDateTime.now());
            surveyMapper.save(survey);
        }
    }

    /**
     * 统计每个问题选项次数
     *
     * @return
     */
    @Override
    public String queryCount() {
        List<String> list = surveyMapper.getQuestionName();
        HashMap<String, Integer> hm = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        for (String question : list) {

            if (question.equals("2.您的性别是？")) continue;
            List<String> quesList = surveyMapper.getAnswer(question);
            if (question.charAt(0) > '9' || question.charAt(0) < '0') {
                question = "1." + question;
            }
            sb.append(question).append("<br>");
            for (String answer : quesList) {
                if (answer.split("; ").length == 1) {
                    if (hm.containsKey(answer)) {
                        hm.put(answer, hm.get(answer) + 1);
                    } else {
                        hm.put(answer, 1);
                    }
                } else {
                    String[] str = answer.split("; ");
                    for (String s : str) {
                        if (hm.containsKey(s)) {
                            hm.put(s, hm.get(s) + 1);
                        } else {
                            hm.put(s, 1);
                        }
                    }
                }
            }
            Set<Map.Entry<String, Integer>> entries = hm.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                String answer = entry.getKey();

                if (answer.contains("其他") || answer.equals("无")
                || answer.equals("wu") || answer.equals("暂无")
                || answer.equals("不知道") || answer.contains("原神")
                || answer.equals("都是米哈游干的") || answer.equals("飞行器出行")
                || answer.equals("不清楚") || answer.equals("都行")
                || answer.contains("没") || answer.equals("1")) continue;

                String[] str = answer.split(". ");
                Integer cnt = entry.getValue();
                if (str.length > 1) {
                    sb.append(str[1]).append(" 被选择的次数是: ").append(cnt).append("<br>");
                } else {
                    sb.append(answer).append(" 被选择的次数是: ").append(cnt).append("<br>");
                }
            }
            hm.clear();
        }

        return sb.toString();
    }

    /**
     * 词云
     * @return
     */
    @Override
    public SurveyVO queryWordCloud() {
        List<String> answer_one = surveyMapper.getAnswer(Survey.QUESTION_ONE);
        List<String> answer_two = surveyMapper.getAnswer(Survey.QUESTION_TWO);
        HashMap<String, Integer> hm = new HashMap<>();
        containsWord(answer_one, hm);
        containsWord(answer_two, hm);
        List<String> wordList = new ArrayList<>();
        List<Integer> count = new ArrayList<>();

        List<Integer> value = new ArrayList<>(hm.values());
        Collections.sort(value);
        value.sort(Collections.reverseOrder());

        for (Integer integer : value) {
            if (integer >= 3) count.add(integer);
        }

        String word;
        for (Integer position : count) {
            for (String key : hm.keySet()) {
                if (hm.get(key).equals(position)) {
                    word = turnName(key);
                    if (word == null) continue;
                    wordList.add(word);
                    hm.remove(key);
                    break;
                }
            }
        }

        return SurveyVO.builder()
                .wordList(wordList)
                .count(count)
                .build();
    }

    private String turnName(String key) {
        String name = null;
        if (key.equals("管理")) name = "加强" + key;
        if (key.equals("分流")) name = "进行" + key;
        if (key.equals("停车")) name = "随意" + key;
        if (key.equals("道路")) name = "拓展" + key;
        if (key.equals("路线")) name = "多开发点" + key;
        if (key.equals("公共交通")) name = "提倡" + key;
        if (key.equals("遵守")) name = key + "交通规章制度";
        if (key.equals("红绿灯")) name = "调整" + key + "时间";
        return name;
    }

    /**
     * 统计词云次数
     * @param answer
     * @param hm
     */
    private void containsWord(List<String> answer, HashMap<String, Integer> hm) {
        for (String ao : answer) {
            for (String word : Survey.wordList) {
                if(ao.contains(word) && !hm.containsKey(word)) {
                    hm.put(word, 1);
                } else if (ao.contains(word) && hm.containsKey(word)) {
                    hm.put(word, hm.get(word) + 1);
                }
            }
        }
    }
}
